package com.hz.qs.utils;

import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.Map;

public class MapUtil {
    /**
     * 升序
     *
     * @param oriMap
     * @return java.util.Map<java.lang.String, java.lang.Long>
     * @author yejy
     * @date 2021/1/18 13:58
     */
    public static Map<String, Long> sortMapByValue(Map<String, Long> oriMap) {
        if (oriMap == null || oriMap.isEmpty()) {
            return null;
        }
        LinkedHashMap<String, Long> sortedMap = new LinkedHashMap<>(16);
        oriMap.entrySet().stream().sorted(Map.Entry.comparingByValue()).forEachOrdered(m -> sortedMap.put(m.getKey(), m.getValue()));
        return sortedMap;
    }

//    public static void main(String[] args) {
//        Map<String,Long> map = new HashMap<>();
//        map.put("a",1L);
//        map.put("b",32L);
//        map.put("c",21L);
//        map.put("d",12L);
//        Map<String, Long> longMap = sortMapByValueDesc(map);
//        String s = longMap.keySet().iterator().next();
//        System.out.println(s);
//    }

    /**
     * 降序
     *
     * @param oriMap
     * @return java.util.Map<java.lang.String, java.lang.Long>
     * @author yejy
     * @date 2021/1/18 13:59
     */
    public static Map<String, Long> sortMapByValueDesc(Map<String, Long> oriMap) {
        if (oriMap == null || oriMap.isEmpty()) {
            return null;
        }
        LinkedHashMap<String, Long> sortedMap = new LinkedHashMap<>(16);
        oriMap.entrySet().stream().sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())).forEachOrdered(m -> sortedMap.put(m.getKey(), m.getValue()));
        return sortedMap;
    }
}
